import api from '~api/index'
import util from '~src/lib/util'
export default {
  state: {
    list: {},
    showList: {
      data: [],
      page: 1,
      id:'-1',
      code: '0',
      total:'0'
    },
    show:{}
  },
  mutations: {
    ['setIntegralList'] (state, info) {
      state.list = info
    },
    ['setIntegralShow'] (state, datas) {
      state.show = datas
    },
    ['setIntegralshowList'] (state, data) {
      if (data.code == '200'){
        if (data.page === 1) {
          data.data = [].concat(data.data)
          if (data.data.length === 0) {
            data.data = []
          }
        } else {
          data.data = state.showList.data.concat(data.data)
        }
        state.showList.total = data.total
        state.showList.data = data.data
      }
      state.showList.code = data.code
    }
  },
  actions: {
    async ['getIntegralList'] ({commit}, id) {
      const { data: { data, code } } = await api.post('/v1/bonusGoods',
        {openid: util.localItem.get('openid'), card_id: id})
      if (data && code === '200') {
        commit('setIntegralList', data.goods_list)
      }
    },
    async ['getIntegralshowList'] ({commit}, config) {
      const { data: { data, code } } = await api.post('/v1/bonusLogs',
        {openid: util.localItem.get('openid'), card_id: config.id, page: config.page, page_size: 12})
      if (data && code === '200') {
        commit('setIntegralshowList', {
          data: data.logList.length>0?data.logList:[],
          page: config.page,
          code: code,
          total:data.total
        })
        config.callback && config.callback()
      }else{
        commit('setIntegralshowList', {
          data: [],
          page: config.page,
          code: code
        })
        config.stopCallback && config.stopCallback()
      }
    },
    async ['getIntegralshow'] ({commit}, config) {
      const { data: { data, code } } = await api.post('/v1/bonusDetails',
        {openid: util.localItem.get('openid'), card_id: config.mid,  bonus_id: config.sid})
      if (data && code === '200') {
          if (data.type ==='2') {
            var datas = {
              gs_name:data.exchange_order_info.gs_name,
              qrcode_url:data.exchange_order_info.qrcode_url,
              order_no:data.order_no,
              createTime:data.createTime,
              type:data.type,
              tips:data.tips,
              balance:data.balance,
              last_balance:data.last_balance,
              exchange_code: data.exchange_order_info.exchange_code,
              goodsItemList:data.exchange_order_info.goodsItemList
            }
          } else {
            var datas = {
              order_no:data.order_no,
              createTime:data.createTime,
              type:data.type,
              tips:data.tips,
              balance:data.balance,
              last_balance:data.last_balance
            }
          }

        commit('setIntegralShow', datas)
      }
    },
    async ['IntegralSubmit'] ({commit}, from) {
      const { data: { data, code ,msg} } = await api.post('/v1/bonusExchange',
        {
          openid: util.localItem.get('openid'),
          member_id: from.member_id,
          gs_id: from.gs_id,
          total_num: from.total_num,
          total_bonus: from.total_bonus,
          item: from.item
        })
      if (data && code === '200') {
        commit('TOAST_SHOW', {content: '兑换成功', msg: true})
        location.href = '/card/show?id=' + from.cart_id
      }else{
        commit('TOAST_SHOW', {content: msg, msg: true})
      }
    }
  },
  getters: {
    ['getIntegralList'] (state) {
      return state.list
    },
    ['getIntegralshowList'] (state) {
      return state.showList
    },
    ['getIntegralshow'] (state) {
      return state.show
    }
  }
}
